﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class updateTypeCard : System.Web.UI.Page
{
    bussiness bus;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bus = new bussiness();
            List<TypeEcard> lstTypeEcard = bus.getAllTypeEcard();
            GridView1.DataSource = lstTypeEcard;
            GridView1.DataBind();
        }

    }
    public void load_data()
    {
        bus = new bussiness();
        List<TypeEcard> lstTypeEcard = bus.getAllTypeEcard();
        GridView1.DataSource = lstTypeEcard;
        GridView1.DataBind();
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {

    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        load_data();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        load_data();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        bus = new bussiness();
        int id = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
        List<Ecard> lstEcard = bus.getAllEcard();
        bool flag = true;


        foreach (Ecard ed in lstEcard)
        {
            if (ed.TypeId == id)
            {
                flag = false;
                break;
            }
        }
        if (flag)
        {
            bus.deleteTyeEcard(id);
            load_data();
            lblInfo.Text = "";
        }
        else
        {
            //Response.Write("<script>alert('Type Card existed 1 foreign key')</script>");
            lblInfo.Text = "Type Card existed 1 foreign key";
        }

    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

        bus = new bussiness();
        int id = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
        List<Ecard> lstEcard = bus.getAllEcard();
        string nameTypeEcard = (GridView1.Rows[e.RowIndex].Cells[1].Controls[0] as TextBox).Text;
        if (nameTypeEcard == "")
        {
            lblInfo.Text = "Type Name must is not null";
            load_data();
        }
        else
        {
            bus.updateTypeEcard(id, nameTypeEcard);
            GridView1.EditIndex = -1;
            load_data();
            lblInfo.Text = "";
        }


    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        bus = new bussiness();
        List<TypeEcard> lstTypeEcard = bus.getAllTypeEcard();
        bool flag = true;
       
        if (bus.checkNull(txtNameEcardType.Text))
        {
            flag = false;
            lblInfo.Text = "Name Ecard Type must is not null";
        }
        else
        {
            foreach (TypeEcard te in lstTypeEcard)
            {
                if (te.TypeCardName == txtNameEcardType.Text)
                {
                    flag = false;
                    lblInfo.Text = "Name Ecard Type is existed";
                    break;
                }
            }
        }
        if (flag)
        {
            bus.insertTypeEcard(txtNameEcardType.Text);
            txtNameEcardType.Text = "";
            lblInfo.Text = "";
            load_data();
        }

    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        load_data(); 
    }
}